package tpcorba.exoservdp;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import tpcorba.exoservdp.servdp.CleAbsente;
import tpcorba.exoservdp.servdp.CleDejaPresente;
import tpcorba.exoservdp.servdp.CleNonValide;
import tpcorba.exoservdp.servdp.ServDPItf;
import tpcorba.exoservdp.servdp.ServDPItfHelper;
import tpcorba.exoservdp.servdp.octetSetHolder;


/**
 * Applications reparties
 * TP - Exercice
 * ServDP
 * 
 * Programme principal du client
 * 
 * @author Toto
 * @version 1.0
 */
public class ServDPclient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try
		{
			//Initialisation de l'ORB
			ORB orb = ORB.init(args, null);

			//Recuperation de la reference de l'objet mandataire (proxy) du service de nommage 
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			//Recuperation de la reference du servant grace au service de nommage 
			String name = "servdp";
			ServDPItf servdp = ServDPItfHelper.narrow(ncRef.resolve_str(name));

			System.out.println(servdp.nbreCouples()+" couples enregistres");
			
			byte[] tab = {1,0,1};
			servdp.deposer("test",tab);
			
			System.out.println(servdp.nbreCouples()+" couples enregistres");
			
			octetSetHolder h = new octetSetHolder();
			servdp.lire("test",h);
			byte[] recup = h.value;
			for(int i=0; i<recup.length; i++) System.out.print(recup[i]+" ");
			System.out.print("\n");
			
		} 
		catch (InvalidName e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		} 
		catch (NotFound e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		} 
		catch (CannotProceed e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		} 
		catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		} catch (CleDejaPresente e) {
			e.printStackTrace();
		} catch (CleNonValide e) {
			e.printStackTrace();
		} catch (CleAbsente e) {
			e.printStackTrace();
		} 
	}

}
